package com.afei.filepicker;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

public class FilePickerActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "FilePickerActivity";
    public static final String INTENT_EXTRA_CHOOSE_PATHS = "paths";

    private TextView mFilepathTv;
    private EmptyRecyclerView mRecyclerView;
    private FileAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_picker);
        initView();
    }

    private void initView() {
        findViewById(R.id.toolbar_left_iv).setOnClickListener(this);
        findViewById(R.id.toolbar_right_iv).setOnClickListener(this);
        findViewById(R.id.back_iv).setOnClickListener(this);

        mFilepathTv = findViewById(R.id.path_tv);
        mRecyclerView = findViewById(R.id.file_recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setItemAnimator(null);
        String path = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFilepathTv.setText(path);
        mAdapter = new FileAdapter(this, path);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setEmptyView(findViewById(R.id.empty_view));
        mAdapter.setOnDirChangeListener(new FileAdapter.OnDirChangeListener() {
            @Override
            public void onDirChangeListener(File currentDirectory) {
                mFilepathTv.setText(currentDirectory.getAbsolutePath());
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_left_iv:
                chooseCancel();
                break;
            case R.id.toolbar_right_iv:
                chooseDone();
                break;
            case R.id.back_iv:
                onBackPressed();
                break;
        }
    }

    private void chooseCancel() {
        setResult(RESULT_CANCELED, null);
        finish();
    }

    private void chooseDone() {
        ArrayList<String> choosePaths = mAdapter.getChoosePaths();
        if (choosePaths == null || choosePaths.size() <= 0) {
            chooseCancel();
        }
        Log.d(TAG, "choosePaths length: " + choosePaths.size());
        Intent intent = new Intent();
        intent.putStringArrayListExtra(INTENT_EXTRA_CHOOSE_PATHS, choosePaths);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onBackPressed() {
        if (mAdapter.isChooseMode()) {
            mAdapter.quitMode();
            return;
        }
        if (!mAdapter.isRootDir()) {
            mAdapter.backParent();
            return;
        }
        chooseCancel();
    }
}